package com.pussinboots.morning.sync.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pussinboots.morning.sync.dao.ProductAttributeMapper;
import com.pussinboots.morning.sync.entity.ErpProductView;
import com.pussinboots.morning.sync.entity.Product;
import com.pussinboots.morning.sync.entity.ProductAttribute;
import com.pussinboots.morning.sync.service.IProductAttributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品属性表 服务实现类
 * </p>
 *
 * @author BaoZhou
 * @since 2019-09-10
 */
@Service
@Slf4j
public class ProductAttributeServiceImpl extends ServiceImpl<ProductAttributeMapper, ProductAttribute> implements IProductAttributeService {
    @Autowired
    private ProductAttributeMapper productAttributeMapper;

    @Override
    public boolean updateStockBatchByErp(List<ErpProductView> erpProductList) {
        try {
            productAttributeMapper.updateStockBatchByErp(erpProductList);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

    @Override
    public boolean updateBatchByProduct(List<Product> productList) {
        try {
            productAttributeMapper.updateStockBatchByProduct(productList);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

    @Override
    public boolean insertStockBatchByProduct(List<Product> productList) {
        List<ProductAttribute> attributes = new ArrayList<>();
        ProductAttribute attribute;
        for (Product product : productList) {
            if (product.getProductId()!=null) {
                attribute = ProductAttribute.builder()
                        .stock(product.getStock())
                        .productId(product.getProductId())
                        .build();
                attributes.add(attribute);
            }
        }
        saveBatch(attributes);
        return false;
    }

    @Override
    public boolean delStockByErpId(List<String> erpIds) {
        try {
            productAttributeMapper.delStockByErpId(erpIds);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }
}